<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="../../../attr.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!doctype html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=emulateIE7" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8">
<link rel="stylesheet" href="${ctx}/css/sapar.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/css/common.css" />
<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/sapar.js"></script>
<script type="text/javascript" src="${ctx}/js/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/assets/layer/layer.js"></script>
<title>编辑公告</title>
</head>

<body>
    <div id="saper-container">
        <div id="saper-hd"></div>
        <div id="saper-bd">
            <div class="subfiled clearfix">
                <h2>编辑公告</h2>
            </div>
			<div class="subfiled-content">
	            <form class="saper-form" id="dataForm">
	                <c:if test="${affiche != null}">
		                <div class="kv-item clearfix">
		                    <label><span class="impInfo">*</span>公告标题：</label>
		                    <div class="kv-item-content">
		                        <select id="affsort" name="afficheSort">
		                            <option value="2" <c:if test="${affiche.afficheSort==2}">selected='selected'</c:if>>通告</option>
		                            <option value="1" <c:if test="${affiche.afficheSort==1}">selected='selected'</c:if>>排班</option>
		                        </select>
		                    </div>
		                </div>
		            </c:if>
	                <div class="kv-item clearfix">
	                    <label><span class="impInfo">*</span>公告标题：</label>
	                    <div class="kv-item-content">
	                        <input type="text" placeholder="公告标题" class="txt_required" name="afficheTitle" value="${affiche.afficheTitle}" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')">
	                    </div>
	                </div>
	                <div class="kv-item clearfix">
	                    <label><span class="impInfo">*</span>公告内容：</label>
	                    <div class="kv-item-content">
	                    	<textarea rows="1" cols="10"  placeholder="公告内容" class="txt_required" name="afficheContent" value="${affiche.afficheContent}">${affiche.afficheContent}
	                    	</textarea>
	                    </div>
	                </div>
					<div class="kv-item clearfix">
	                    <label>相关图片 ：</label>
	                    <div class="kv-item-content">
							<form action="/upload/uploadfile" method="POST" enctype="multipart/form-data">
								文件：<input type="file" name="test"/>
								<input type="submit" value="上传" />
							</form>
	                    	<input type="hidden" name="afficheAdd" id="afficheAdd" value="${affiche.afficheAdd}"/>
	                    	<input type="file" name="upPicFile" id="upPicFile" value="${affiche.afficheAdd} ">
	                        <input type="button" id="uploadPic" value="上传" />
	                    </div>
	                </div>
	                <div class="buttons">
	                    <a href="javascript:;" class="sapar-btn sapar-btn-recom">确定</a>
	                    <input name="afficheId" type="hidden" value="${affiche.afficheId}">
	                </div>
	            </form>
            </div>
        </div>
        <div id="saper-ft"></div>
    </div>
    

<script src="${ctx}/js/ajaxfileupload.js"></script>
<script type="text/javascript">
function isNullOrEmpty(strVal){
	strVal = strVal.replace(/(^\s*)|(\s*$)/g,"");
	if(strVal == '' || strVal == null || strVal == undefined){
		return true;
	}else{
		return false;
	}
}
$(function(){
	$(".sapar-btn-recom").click(function(){
		 var btn = true;
		 $(".txt_required").each(function(){
			 var str =$(this).val();
			 if(isNullOrEmpty(str)){
				 btn = false;
				 layer.msg("信息不能为空");
				 return false;
			 }
		 });
		 var leixing = $("#affsort").val();
		 var upfile = $("#afficheAdd").val();
		 if(leixing == 1){
			 if(isNullOrEmpty(upfile)){
				 layer.alert("请选择排班信息");
				 btn = false;
				 return btn;
			 }
		 }
		 if(btn){
			 $.ajax({
	             type: "POST",
	             url:"${ctx}/affiche/afficheSave.do",
	             data:$('#dataForm').serialize(),// 你的formid
	             error: function(request) {
	            	 	layer.alert("网络故障，请稍后再试!");
	             },
	             success: function(data) {
	 	           	if(data.status=="true"){
		 	           	layer.alert("保存成功", function(){
		 	           		window.location.href="${ctx}/affiche/afficheList.do";
	           		    });
	 	           	}else{
	 	           		 layer.msg(data.msg);
	 	           	}
	             }
	         }); 
		 }
	});
	
	$("#uploadPic").click(function () {
  		 if($("#upPicFile").val()==''){
  			 layer.msg("请选择图片");
			 return false;
  		 }
        ajaxFileUpload();
   });
   
   function ajaxFileUpload() {
	   var i =1;
       $.ajaxFileUpload({
               url: '${ctx}/upload/uploadfile?test='+i, //用于文件上传的服务器端请求地址
               secureuri: false, //是否需要安全协议，一般设置为false
               fileElementId: 'upPicFile', //文件上传域的ID
               dataType: 'json', //返回值类型 一般设置为json
               success: function (data, status){  //服务器成功响应处理函数
               	var msg = data.message;
               	if(data.error==1){
               		$("#afficheAdd").val(msg);
               		layer.alert('上传成功', {
                	    skin: 'layui-layer-lan'
                	    ,closeBtn: 0
                	    ,shift: 4 //动画类型
	                });
               	}else{
               		layer.alert('上传失败,'+msg, {
                	    skin: 'layui-layer-lan'
                	    ,closeBtn: 0
                	    ,shift: 4 //动画类型
	                });
               	}
               	
               },
               error: function (data, status, e){//服务器响应失败处理函数
                   alert("上传失败");
               }
           })
       return false;
   }
});
</script>
</body>
</html>